package com.yzy.fly.entity.core;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yzy.fly.annotation.OrderSeq;
import com.yzy.fly.config.SpringContextUtil;
import com.yzy.fly.kernel.model.FlyBasicModel;
import com.yzy.fly.mapper.FlySqlMapper;
import com.yzy.fly.kernel.model.DataIncrement;
import com.yzy.fly.util.DateUtil;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
@TableName(value = "fly_sql", autoResultMap = true)
public class FlySql extends FlyBasicModel<Integer,FlySql>{
    @TableId(type = IdType.AUTO)
    private Integer id;
    @OrderSeq
    private Integer seq;
    private String packageName;
    private String className;
    private String label;
    private String name;
    private String sqlId;
    private String type;
    private String dataSource;
    private String databaseType;
    private String sqlContent;

    @TableField(value = "params",typeHandler = FastjsonTypeHandler.class)
    private JSONArray params = new JSONArray(0);
    @TableField(value = "result",typeHandler = FastjsonTypeHandler.class)
    private JSONArray result = new JSONArray(0);
    private String returnClass;
//    @ParentId
    private Integer componentId;
    private Integer systemId;
    private String sqlDesc;
    private String descr;
    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    private Date createTime;
    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    private Date updateTime;

    @Override
    public boolean idSame(FlySql item) {
        if (null != id && (item.getId().intValue() == id.intValue() && -1 != item.getId().intValue() )) {
            return true;
        }
        return false;
    }

//    @Override
//    public boolean insert() {
//        FlySqlMapper flySqlMapper = SpringContextUtil.getBean("flySqlMapper");
//        flySqlMapper.insert(this);
//        return true;
//    }

    @Override
    public boolean updateById() {
        FlySqlMapper flySqlMapper = SpringContextUtil.getBean("flySqlMapper");
        flySqlMapper.updateById(this);
        return true;
    }

    @Override
    public boolean deleteById() {
        FlySqlMapper flySqlMapper = SpringContextUtil.getBean("flySqlMapper");
        flySqlMapper.deleteById(id);
        return true;
    }
}
